﻿using System.Collections.Generic;
using System.Data;
using IntelligentStorWpfApp.Interface;
using IntelligentStorWpfApp.Model;
using MySql.Data.MySqlClient;
using IntelligentStorWpfApp.Helper;

namespace IntelligentStorWpfApp.Services
{
    public class OperationLogServices : OperationLogInterface
    {
        string sqlStr = "";
        public bool AddLog(OperationLog opLog)
        {
            sqlStr = "INSERT INTO c_operation_log(UserName, OperatTime, Actions) VALUES (@UserName,@OperatTime,@Actions);";

            MySqlParameter[] paras =
            {
                new MySqlParameter ("@UserName",opLog.UserName),
                new MySqlParameter ("@OperatTime",opLog.OperatTime),
                new MySqlParameter ("@Actions",opLog.Actions)
            };
            return DBHelper.ExecuteNonQuery(sqlStr, paras);

        }

        public DataTable GetAllLog()
        {
            sqlStr = "select * from c_operation_log;";
            return DBHelper.ExecuteDataSet(sqlStr, null);
        }

        public DataTable getLogByUserAndTime(string UserName, string startTime, string endTime)
        {
            sqlStr = "select * from c_operation_log";

            List<MySqlParameter> listmysqlPram = new List<MySqlParameter>();
            if(!string.IsNullOrEmpty(UserName)||!string.IsNullOrEmpty(startTime.ToString()) || !string.IsNullOrEmpty(endTime.ToString()) )
            {
                sqlStr = sqlStr + " where 1=1 ";
                if(!string.IsNullOrEmpty(UserName))
                {
                    sqlStr = sqlStr + "And UserName like @UserName ";
                    UserName = "%" + UserName + "%";
                    listmysqlPram.Add(new MySqlParameter("@UserName", UserName));
                }
                if(!string.IsNullOrEmpty(startTime))
                {
                    sqlStr = sqlStr + "And OperatTime>@startTime ";
                    listmysqlPram.Add(new MySqlParameter("@startTime", startTime));
                }
                if(!string.IsNullOrEmpty(endTime))
                {
                    sqlStr = sqlStr + "And OperatTime<@endTime ";
                    listmysqlPram.Add(new MySqlParameter("@endTime", endTime));
                }
                MySqlParameter[] para = listmysqlPram.ToArray();

                return DBHelper.ExecuteDataSet(sqlStr, para);
            }
            else
            {
                return DBHelper.ExecuteDataSet(sqlStr, null);
            }
        }
    }
}
